<!--
  * 实训室耗材申请
  *
  * @Author:    李韵妍
  * @Date:      2024-11-08 13:31:04
  * @Copyright  🐂🐎bloc
-->
<template>
  <a-modal
      :title="form.id ? '编辑' : '添加'"
      :width="800"
      :open="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }" >

        <a-form-item label="申请人姓名"  name="actualName">
<!--          <EmployeeSelect ref="employeeSelect" placeholder="请输入申请人姓名" width="100%"-->
<!--             v-model:value="form.actualName"/>-->
          <a-input style="width: 100%" v-model:value="form.actualName" placeholder="申请人姓名" disabled/>
        </a-form-item>
        <a-form-item label="申请班级"  name="classroom">
          <a-input style="width: 100%" v-model:value="form.classroom" placeholder="申请班级" />
        </a-form-item>
        <a-form-item label="借用设备名称"  name="equipmentName">
          <a-input v-if="form.id" style="width: 100%" v-model:value="form.equipmentName" placeholder="借用设备的名称"  disabled/>
          <a-select v-else style="width: 100%" @change="handleChange" label-in-value placeholder="借用设备的名称" v-model:value="eqValue" :options="eqList">
          </a-select>
        </a-form-item>
        <a-form-item label="借用设备规格"  name="specificationModel">
          <a-input style="width: 100%" v-model:value="form.specificationModel" placeholder="借用设备的规格型号" :disabled="form.id"/>
        </a-form-item>
<!--        <a-form-item label="借用设备的数量"  name="quantity">-->
<!--          <a-input-number style="width: 100%" v-model:value="form.quantity" placeholder="借用设备的数量" />-->
<!--        </a-form-item>-->
      <a-form-item label="详细的设备借用理由"  name="borrowReason">
        <a-textarea style="width: 100%" v-model:value="form.borrowReason" placeholder="详细的设备借用理由" />
      </a-form-item>
      <a-form-item label="借用日期"  name="borrowStartDate">
        <a-date-picker valueFormat="YYYY-MM-DD" v-model:value="form.borrowStartDate" style="width: 100%" placeholder="借用日期"/>
      </a-form-item>
      <a-form-item label="归还日期"  name="borrowEndDate">
        <a-date-picker valueFormat="YYYY-MM-DD" v-model:value="form.borrowEndDate" style="width: 100%" placeholder="归还日期"/>
      </a-form-item>

        <a-form-item label="备注信息"  name="remark">
        <a-textarea style="width: 100%" v-model:value="form.remark" placeholder="关于设备借用的其他备注信息" />
        </a-form-item>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
import {reactive, ref, nextTick, onMounted, watch} from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { equipmentBorrowApplicationApi } from '/@/api/business/equipment-borrow-application/equipment-borrow-application-api';
  import { smartSentry } from '/@/lib/smart-sentry';
  import EmployeeSelect from '/@/components/system/employee-select/index.vue';
import {useUserStore} from "/@/store/modules/system/user.js";


  // ------------------------ 数据 ------------------------

  const eqStatus = 0; // 设备状态 0:可用
  const eqList = ref([]);
  const eqResult = ref([]);
  const userStore = useUserStore();


  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    // console.log('user',userStore)
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
      // console.log('row',rowData)
    }
    if(!form.id){
      form.actualName = userStore.actualName
      form.applicant = userStore.employeeId
    }
    // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
    // if (form.status && form.status.length > 0) {
    //   form.status = form.status.map((e) => e.valueCode);
    // }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const employeeSelect = ref();

  const formDefault = {
      id: undefined, // 主键id
      borrowReason: undefined, //详细的设备借用理由
      applicant: undefined, //申请人姓名
      actualName: undefined, //实际姓名
      classroom: undefined, //申请班级
      borrowStartDate: undefined, //设备借用开始日期
      borrowEndDate: undefined, //设备借用结束日期
      equipmentName: undefined, //借用设备的名称
      specificationModel: undefined, //借用设备的规格型号
      quantity: undefined, //借用设备的数量
      borrowStatus: undefined, //设备当前的借出状态，1表示已借出，0表示未借出
      remark: undefined, //关于设备借用的其他备注信息
      guideTeacherSignature: undefined, //指导老师签字信息，可以是图片路径或其他标识
      adminSignature: undefined, //管理员签字信息，可以是图片路径或其他标识
      borrowDate: undefined, //设备实际借出日期
      returnConfirmation: undefined, //设备是否已确认归还，1表示已确认，0表示未确认
      returnDate: undefined, //设备归还日期
      returnStatus: undefined, //设备当前的归还状态，1表示已归还，0表示未归还
      equipmentList: [], // 实训室所有可用设备
      equipmentId: undefined, // 设备id

  };

  let form = reactive({ ...formDefault });

  const rules = {
      borrowReason: [{ required: true, message: '详细的设备借用理由 必填' }],
      actualName: [{ required: true, message: '申请人姓名 必填' }],
      classroom: [{ required: true, message: '申请班级 必填' }],
      borrowStartDate: [{ required: true, message: '设备借用开始日期 必填' }],
      borrowEndDate: [{ required: true, message: '设备借用结束日期 必填' }],
      equipmentName: [{ required: true, message: '借用设备的名称 必填' }],
      specificationModel: [{ required: true, message: '借用设备的规格型号 必填' }],
      quantity: [{ required: true, message: '借用设备的数量 必填' }],
      borrowStatus: [{ required: true, message: '设备当前的借出状态，1表示已借出，0表示未借出 必填' }],
      remark: [{ required: true, message: '关于设备借用的其他备注信息 必填' }],
      guideTeacherSignature: [{ required: true, message: '指导老师签字信息，可以是图片路径或其他标识 必填' }],
      adminSignature: [{ required: true, message: '管理员签字信息，可以是图片路径或其他标识 必填' }],
      borrowDate: [{ required: true, message: '设备实际借出日期 必填' }],
      returnConfirmation: [{ required: true, message: '设备是否已确认归还，1表示已确认，0表示未确认 必填' }],
      returnDate: [{ required: true, message: '设备归还日期 必填' }],
      returnStatus: [{ required: true, message: '设备当前的归还状态，1表示已归还，0表示未归还 必填' }],
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    try {
      if (form.id) {
        await equipmentBorrowApplicationApi.update(form);
      } else {
        await equipmentBorrowApplicationApi.add(form);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  // ------------------------ 实训室所有可用设备------------------------

  async function loadEquipmentList(status) {
    try {
      const res = await equipmentBorrowApplicationApi.getAllEquipment(status);
      if (res && res.data) {
        eqResult.value = res.data;
        eqList.value = res.data.map((_)=>{
          return {
            label: _.equipmentName,
            value: _.id,
          }
        });

      }
    } catch (err) {
      smartSentry.captureError(err);
    }
  }

  const eqValue = ref({
  label: form.equipmentName,
  value:form.equipmentId,
  });

  function handleChange(value) {
    form.equipmentId = value.value;
    form.equipmentName = value.label;
    console.log(form.equipmentName);

  }

  watch(()=>form.equipmentName,(newValue,oldValue)=>{
    if (newValue) {
      const selectedEquipment = eqResult.value.find((item) => item.equipmentName === newValue);
      if (selectedEquipment) {
        form.specificationModel = selectedEquipment.specificationModel;
      }
    }
  })

   onMounted(()=> {
     loadEquipmentList(eqStatus)
   })

  defineExpose({
    show,
  });
</script>
